import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger

# 导入配置
from config.settings import settings

# 创建FastAPI应用
app = FastAPI(
    title="AI大模型二次开发平台",
    description="集成模型加载、微调、RAG检索的AI平台，兼容OpenAI API格式",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
@app.on_event("startup")
async def startup_event():
    """应用启动事件处理函数"""
    logger.info("AI大模型二次开发平台启动中...")
    
    # 导入并注册API路由
    from api.router import router as api_router
    app.include_router(api_router, prefix="/api/v1")
    
    # 导入并注册OpenAI兼容API路由
    from api.openai_compatible_router import openai_compatible_router
    app.include_router(openai_compatible_router, prefix="/v1")
    
    logger.info("AI大模型二次开发平台启动成功！")

@app.get("/")
def root():
    """根路径接口"""
    return {
        "status": "success",
        "message": "AI大模型二次开发平台运行中",
        "version": "1.0.0"
    }

if __name__ == "__main__":
    # 启动服务
    uvicorn.run(
        "main:app",
        host=settings.HOST,
        port=settings.PORT,
        reload=settings.RELOAD,
        workers=settings.WORKERS,
        log_level=settings.LOG_LEVEL
    )